package com.kingsystem.kingfinancial_backend.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingsystem.kingfinancial_backend.entity.Users;
import com.kingsystem.kingfinancial_backend.mapper.UsersMapper;
import com.kingsystem.kingfinancial_backend.service.impl.UsersServiceImpl;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.awt.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author leiyue_yao
 * @since 2025-07-11
 */
@Controller
@RequestMapping("/users")
@CrossOrigin
public class UsersController {

    @Autowired
    private UsersMapper um;

    @Autowired
    private UsersServiceImpl usi;

    @PostMapping("/checkUser")
    @ResponseBody
    public Users checkUser(@RequestParam("user_name") String user_name,
                           @RequestParam("password") String password){
        QueryWrapper<Users> qw = new QueryWrapper<>();
        qw.eq("user_name", user_name)
                .eq("password", password);

        Users user = um.selectOne(qw);

        user = usi.getOne(qw);

        return user;
    }


    @PostMapping("/addUser")
    @ResponseBody
    public String addUser(@RequestBody Users user){
        try{
            usi.save(user);
            return "success";
        }
        catch(Exception ex){
            return ex.getMessage();
        }
    }


    @PostMapping("/addUser2")
    @ResponseBody
    public String addUser2(@RequestParam("userInfo") String userInfo){
        try{
            JSONObject jo = JSON.parseObject(userInfo);
            String realname = jo.getString("realName");

            Users user = new Users();
            user.setTableKey(jo.getString("tableKey"));

            usi.save(user);
        }
        catch(Exception ex){
            return ex.getMessage();
        }
        return "aaa";
    }
}
